<?php

/**
 * @encoding     :   UTF-8
 * @author      :   2013-1-21 16:52:40 by Lsl8966 , 470663151@qq.com , http://www.cnblogs.com/lsl8966
 * @Package     :
 * @version   
 */

/**
 * @param string $server 服务器地址
 * @param string $curlPost post数据，如：name=toy&age=22
 * @return string 页面返回值
 */
function post($server,$curlPost){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $server);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 80);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}


function getClientIp(){
    if (getenv("HTTP_CLIENT_IP")){
        $ip = getenv("HTTP_CLIENT_IP");
    }else if(getenv("HTTP_X_FORWARDED_FOR")){
        $ip = getenv("HTTP_X_FORWARDED_FOR");
    }else if(getenv("REMOTE_ADDR")){
        $ip = getenv("REMOTE_ADDR");
    }else {
        $ip = '';
    }

    return $ip;
}


/**
 * 获取指定商品卖出数量
 */
function getProductSlodNum($product_id){
    $model = Controller::createMode('db_sys_26_29','t_26_order_product_list');
    if($product_id){
        $map['m_id'] = $product_id;
        $dt =  $model->from('t_26_order_product_list', array('slod_num'=>'sum(total)'))->where($map)->select();
        return $dt[0]['slod_num']? $dt[0]['slod_num']:0;
    }
}

/**
 * @param $type_id
 * @return string订单状态|0=未支付|1=待收货|2=待评价|3=退货|4=已支付待发货|5=货到付款待发货|6=已评论
 */
function getOrderType($type_id){
    switch($type_id){
        case 0:
            return '未支付';break;
        case 1:
            return '待收货';break;
        case 2:
            return '待评价';break;
        case 3:
            return '退货';break;
        case 4:
            return '已支付待发货';break;
        case 5:
            return '货到付款待发货';break;
        case 6:
            return '已完成评论';break;
        default:
            return '未支付订单';
            break;
    }
}


function getProductKindNameRecu($kid){
    $map['kind_id'] = intval($kid);
    $model = BaseController::model('t_26_product_kind');
    $str = '';
    $i = 0;
    $array = $model->find($map['kind_id']);
    $kname = $array['kind_name'];
    if($array['parent_id'] == 0){
        return $kname;
    }
    do{
        $array = $model->find($array['parent_id']);
        $kind_name = $array['kind_name']?$array['kind_name']:'';
        $map['parent_id'] = intval($array['kind_id']);
        if($kind_name!=''){
            $str = $kind_name . '->' . $str;
        }
        $i++;
    }while($array['kind_id'] > 0 && $i < 20);
    return $str.$kname;
}

function getProductParentKindNameRecu($kid,$mode=null){
    $map['kind_id'] = intval($kid);
    if(!$mode){
        $model = BaseController::createMode('t_26_product_kind');
    }
    $str = '';
    $i = 0;
    $array = $model->find($map['kind_id']);
    $kname = $array['kind_name'];
    if($array['parent_id'] == 0){
        return $kname;
    }
    do{
        $array = $model->find($array['parent_id']);
        $kind_name = $array['kind_name']?$array['kind_name']:'';
        $map['parent_id'] = intval($array['kind_id']);
        if($kind_name!=''){
            $str = $kind_name;
        }
        $i++;
    }while($array['kind_id'] > 0 && $i < 10);
    return $str;
}

function getProductParentKindIdRecu($kid,$mode=null){
    $map['kind_id'] = intval($kid);
    if(!$mode){
        $model = BaseController::createMode('t_26_product_kind');
    }
    $str = 0;
    $i = 0;
    $array = $model->find($map['kind_id']);
    $kid = $array['kind_id'];
    if($array['parent_id'] == 0){
        return $kid;
    }
    do{
        $array = $model->find($array['parent_id']);
        $kind_id = $array['kind_id']?$array['kind_id']:'';
        $map['parent_id'] = intval($array['kind_id']);
        if($kind_id!=''){
            $str = $kind_id;
        }
        $i++;
    }while($array['kind_id'] > 0 && $i < 10);
    return $str;
}

/**
 *
 */
function g2kilogram($num){
    return $num;
}

function g2kilogramNew($num,$spec_id){
    if($spec_id == 0){
        return $num/500;
    } else {
        return $num;
    }
}

function getProductTagName($id){
    if($id ==0) return '普通商品';
    if($id ==1) return '<font color=red>特惠商品</font>';
    if($id ==2) return '<font color=green>团购商品</font>';
}

function getProductSpecName($spec_id){
    $model = BaseController::model('t_26_spec_info');
    $data = $model->getOne(array('spec_id'=>$spec_id));
    return $data['spec_name'];
}

function getPromotionTitle($spec_id){
    $model = Controller::createMode('db_sys_26_71','t_26_product_promotion_kind');
    $data = $model->getOne(array('id'=>$spec_id));
    return $data['title'];
}